﻿@{
    ViewData["Title"] = "Home Page";
}

@* <div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div> *@

<div class="panel panel-default">
    <div class="panel-heading">生成应用</div>
    <div class="panel-body">
        <div class="row">
            <form class="form-horizontal" role="form">
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="orderid">AppId</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="appId" type="text" placeholder="请输入应用AppId" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="orderid">Secret</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="secret" type="text" placeholder="请输入应用Secret" />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 text-center">
                        <input type="button" class="btn btn-success" value="重新生成" onclick="GetGenerate()">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">Token</div>
    <div class="panel-body">
        <div class="row">
            <div class="form-group">
                <div class="col-sm-10 text-center">
                    <input type="button" class="btn btn-success" value="获取Token" onclick="GetToken()">
                    <input type="button" class="btn btn-success" value="验证Token" onclick="GetVerifyToken()">
                    <input type="button" class="btn btn-success" value="刷新Token" onclick="GetRefreshToken()">
                </div>
            </div>
            <form class="form-horizontal" role="form">
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="orderid">令牌（access_token）</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="accessToken" type="text" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="orderid">令牌过期时长(access_expire，单位：分钟)</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="accessExpire" type="text" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="orderid">刷新令牌（refresh_token）</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="refreshToken" type="text" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="orderid">刷新令牌过期时长(refresh_expire，单位：天)</label>
                    <div class="col-sm-10">
                        <input class="form-control" id="refreshExpire" type="text" />
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts {
    <script type="text/javascript">

        $(document).ready(function(){
            GetGenerate();
        });

        function GetGenerate() {
            //var appId = $("#appId").val();
            //var secret = $("#secret").val();
            $.ajax({
                type: "GET",
                url: "/api/app/get",
                //data: { appId: appId, appSecret: secret },
                dataType: "json",
                success: function (res) {
                    if(res) {
                        if(res.success) {
                            if(res.data) {
                                $("#appId").val(res.data.appId);
                                $("#secret").val(res.data.appSecret);
                            }
                            else {
                                alert("未返回数据！");
                            }
                        }
                        else {
                            alert("生成失败！原因：" + res.msg);
                        }
                    }
                    else {
                        alert("请求失败");
                    }
                }
            });
        }

        function GetToken() {
            var appId = $("#appId").val();
            var secret = $("#secret").val();
            $.ajax({
                type: "GET",
                url: "/api/token/get",
                data: { appId: appId, appSecret: secret },
                dataType: "json",
                success: function (res) {
                    console.log(res);
                    if(res) {
                        if(res.success) {
                            if(res.data) {
                                $("#accessToken").val(res.data.access_token);
                                $("#accessExpire").val(res.data.access_expire);
                                $("#refreshToken").val(res.data.refresh_token);
                                $("#refreshExpire").val(res.data.refresh_expire);
                            }
                            else {
                                alert("未返回数据！");
                            }
                        }
                        else {
                            alert("生成失败！原因：" + res.msg);
                        }
                    }
                    else {
                        alert("请求失败");
                    }
                }
            });
        }

        function GetVerifyToken() {
            var accessToken = $("#accessToken").val();
            $.ajax({
                type: "GET",
                url: "/api/token/verify",
                data: { accessToken: accessToken },
                dataType: "json",
                success: function (res) {
                    console.log(res);
                    if(res) {
                        if(res.success) {
                            if(res.data) {
                                console.log(res.data);
                            }
                            else {
                                alert("未返回数据！");
                            }
                        }
                        else {
                            alert("生成失败！原因：" + res.msg);
                        }
                    }
                    else {
                        alert("请求失败");
                    }
                }
            });
        }

        function GetRefreshToken() {
            var refreshToken = $("#refreshToken").val();
            $.ajax({
                type: "GET",
                url: "/api/token/refresh",
                data: { refreshToken: refreshToken },
                dataType: "json",
                success: function (res) {
                    console.log(res);
                    if(res) {
                        if(res.success) {
                            if(res.data) {
                                $("#accessToken").val(res.data.access_token);
                                $("#accessExpire").val(res.data.access_expire);
                            }
                            else {
                                alert("未返回数据！");
                            }
                        }
                        else {
                            alert("生成失败！原因：" + res.msg);
                        }
                    }
                    else {
                        alert("请求失败");
                    }
                }
            });
        }
    </script>
}